package com.lehe.controller;

import com.lehe.entry.SqlParameterDto;
import com.lehe.entry.WithdrawCashDto;
import com.lehe.service.impl.WithdrawCashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

/**
 * 返点提现
 *
 * @author 王丹
 * @since 2018-01-12
 */
@Controller
@RequestMapping("cash")
public class WithdrawCashController extends Basecontroller {

    @Autowired
    WithdrawCashService withdrawCashService;

    /**
     * 首页
     */
    @RequestMapping("index")
    public String index(Model model, Integer year){
        SqlParameterDto sqlParameterDto = new SqlParameterDto();
        LocalDate today = null;
        if(null == year){
            today = LocalDate.now();
            year = today.getYear();
        }else {
            today = LocalDate.of(year, 1, 1);
        }
        String startDate = today.with(TemporalAdjusters.firstDayOfYear()).toString();
        String endDate = today.with(TemporalAdjusters.lastDayOfYear()).toString();
        sqlParameterDto.setUserId(super.getUserId());
        sqlParameterDto.setAgentId(super.getAgentId());
        sqlParameterDto.setStartDate(startDate);
        sqlParameterDto.setEndDate(endDate);
        List<WithdrawCashDto> withdrawCashDtos = withdrawCashService.queryIndex(sqlParameterDto);
        List<Integer> years = new ArrayList<>();
        int loopYear = LocalDate.now().getYear();
        for(int i = loopYear - 4; i <= loopYear; i++){
            years.add(i);
        }
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        model.addAttribute("withdrawCashDto", withdrawCashDtos);
        model.addAttribute("year", year);
        model.addAttribute("years", years);
        return "user/withdrawCash";
    }
//    http://m.qpqpgame.com/wanko_pay/wxpaynotify/TransfersForAgent.php?PayMoney=提现金额（单位元）&URL=返回网址（url编码）
//    http://m.qpqpgame.com/wanko_pay/wxpaynotify/TransfersForAgent.php?PayMoney=1&URL=m.qpqpgame.com

    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        String startDate = today.with(TemporalAdjusters.firstDayOfYear()).toString();
        String endDate = today.with(TemporalAdjusters.lastDayOfYear()).toString();
        System.out.println(startDate);
        System.out.println(endDate);

        int year = today.getYear();
        List<Integer> years = new ArrayList<>();
        for(int i = year - 4; i <= year; i++){
            System.out.println(i);
            years.add(i);
        }
        String s = "2017-01";
        System.out.println(s.substring(5, 7));
    }
}
